package cn.stylefeng.guns.modular.storeProcurement.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;

import cn.stylefeng.guns.modular.storeProcurement.entity.StoreProcurement;
import cn.stylefeng.guns.modular.storeProcurement.mapper.StoreProcurementMapper;
import cn.stylefeng.guns.modular.storeProcurement.model.params.StoreProcurementParam;
import cn.stylefeng.guns.modular.storeProcurement.model.result.StoreProcurementResult;
import cn.stylefeng.guns.modular.storeProcurement.service.StoreProcurementService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 门店采购单 服务实现类
 * </p>
 *
 * @author wp
 * @since 2025-09-02
 */
@Service
public class StoreProcurementServiceImpl extends ServiceImpl<StoreProcurementMapper, StoreProcurement> implements StoreProcurementService {

    @Override
    public void add(StoreProcurementParam param){
        StoreProcurement entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StoreProcurementParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StoreProcurementParam param){
        StoreProcurement oldEntity = getOldEntity(param);
        StoreProcurement newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StoreProcurementResult findBySpec(StoreProcurementParam param){
        return null;
    }

    @Override
    public List<StoreProcurementResult> findListBySpec(StoreProcurementParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StoreProcurementParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public boolean checkCustomerOrder(String orderOutNumber) {
        return this.baseMapper.checkCustomerOrder(orderOutNumber) == null ? false : true;
    }

    @Override
    public LayuiPageInfo findPageByStore(StoreProcurementParam storeProcurementParam, String condition) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findPageByStore(pageContext, storeProcurementParam,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void updateOrderState(StoreProcurementParam storeProcurementParam) {
        this.baseMapper.updateOrderState(storeProcurementParam);
    }

    private Serializable getKey(StoreProcurementParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StoreProcurement getOldEntity(StoreProcurementParam param) {
        return this.getById(getKey(param));
    }

    private StoreProcurement getEntity(StoreProcurementParam param) {
        StoreProcurement entity = new StoreProcurement();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
